package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminSetmealController")
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 分页查询套餐
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result pageSelectSetmeal(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("参数是{}:" , setmealPageQueryDTO);
        PageResult pageResult = setmealService.pageSelectSetmeal(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 添加套餐
     * @param setmealDTO
     * @return
     */

    @PostMapping
    @CacheEvict(cacheNames = "SetmealCache",key = "#setmealDTO.categoryId")
    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO){
        setmealService.addSetmeal(setmealDTO);
        return Result.success();
    }


    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "SetmealCache",allEntries = true)
    public Result deleteSetmeal(@RequestParam List<Long> ids){
        setmealService.deleteSetmeal(ids);
        return Result.success();
    }

    /**
     * 根据id查询回显数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result selectByIdSetmeal(@PathVariable Integer id){
        SetmealDTO setmealDTO = setmealService.selectByIdSetmeal(id);
        return Result.success(setmealDTO);
    }

    /**
     * 根据id或关闭状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result EnableDisable(@PathVariable Integer status,Integer id){
        log.info("传入的数据是:{},{}",status,id);
        setmealService.EnableDisable(status,id);
        return Result.success();
    }

    @PutMapping
    @CacheEvict(cacheNames = "SetmealCache",allEntries = true)
    public Result updateSetmeal(@RequestBody SetmealVO setmealVO){
        setmealService.updateSetmeal(setmealVO);
        return Result.success();
    }

}
